#import "Common.h"
#import "Utils.h"

void gluLookAt(const Vector3 *eyePos,const Vector3 *lookAtPos,const Vector3 *upVector)
{
	Vector3 f,up;
	
	vec3Sub(lookAtPos,eyePos,&f);
	vec3Norm1(&f);
	
	vec3Norm2(upVector,&up);

	Vector3 s,u;
	
	vec3Cross(&f, &up, &s);
	vec3Cross(&s, &f, &u);
	
	vec3Norm1(&s);
	vec3Norm1(&u);
	
	float M[]=
	{
		s.x, u.x, -f.x, 0,
		s.y, u.y, -f.y, 0,
		s.z, u.z, -f.z, 0,
		0, 0, 0, 1
	}; 
	
	glMultMatrixf(M);
	glTranslatef(-eyePos->x, -eyePos->y, -eyePos->z);
}

float applyRange(float min,float max,float value)
{
	if(min > max)
	{
		float tmp = min;
		min = max;
		max = tmp;
	}

	if(value <= min)
		return min;
	
	if(value >= max)
		return max;
	
	return value;
}